/*    */ package scripting.portal;
/*    */

import client.MapleClient;
import configs.ServerConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.MaplePortal;

import javax.script.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */ 
/*    */ public class PortalScriptManager {
/* 18 */   private static final Logger log = LogManager.getLogger(PortalScriptManager.class.getName());
/* 19 */   private static final PortalScriptManager instance = new PortalScriptManager();
/* 20 */   private static final ScriptEngineFactory sef = (new ScriptEngineManager()).getEngineByName("nashorn").getFactory();
/* 21 */   private final Map<String, PortalScript> scripts = new HashMap<>();
/*    */   
/*    */   public static synchronized PortalScriptManager getInstance() {
/* 24 */     return instance;
/*    */   }
/*    */   
/*    */   private PortalScript getPortalScript(String scriptName) {
/* 28 */     if (this.scripts.containsKey(scriptName)) {
/* 29 */       return this.scripts.get(scriptName);
/*    */     }
/* 31 */     File scriptFile = new File(ServerConfig.WORLD_SCRIPTSPATH + "/portal/" + scriptName + ".js");
/* 32 */     if (!scriptFile.exists()) {
/* 33 */       return null;
/*    */     }
/* 35 */     ScriptEngine portal = sef.getScriptEngine();
/*    */     try {
/* 37 */       CompiledScript compiled = ((Compilable)portal).compile(new InputStreamReader(new FileInputStream(scriptFile), "UTF-8"));
/* 38 */       compiled.eval();
/* 39 */     } catch (Exception e) {
/* 40 */       log.error("请检查Portal为:(" + scriptName + ".js)的文件.", e);
/*    */     } 
/* 42 */     PortalScript script = ((Invocable)portal).<PortalScript>getInterface(PortalScript.class);
/* 43 */     this.scripts.put(scriptName, script);
/* 44 */     return script;
/*    */   }
/*    */   
/*    */   public void executePortalScript(MaplePortal portal, MapleClient c) {
/* 48 */     PortalScript script = getPortalScript(portal.getScriptName());
/* 49 */     if (script != null) {
/*    */       try {
/* 51 */         script.enter(new PortalPlayerInteraction(c, portal));
/* 52 */         if (c.getPlayer().isAdmin()) {
/* 53 */           c.getPlayer().dropMessage(5, "执行Portal为:(" + portal.getScriptName() + ".js)的文件 在地图 " + c.getPlayer().getMapId() + " - " + c.getPlayer().getMap().getMapName());
/*    */         }
/* 55 */       } catch (Exception e) {
/* 56 */         if (c.getPlayer().isAdmin()) {
/* 57 */           c.getPlayer().dropMessage(5, "执行地图脚本过程中发生错误.请检查Portal为:( " + portal.getScriptName() + ".js)的文件." + e);
/*    */         }
/* 59 */         log.error("执行地图脚本过程中发生错误.请检查Portal为:( " + portal.getScriptName() + ".js)的文件.", e);
/*    */       } 
/*    */     } else {
/* 62 */       if (c.getPlayer().isAdmin()) {
/* 63 */         c.getPlayer().dropMessage(5, "未找到Portal为:(" + portal.getScriptName() + ".js)的文件 在地图 " + c.getPlayer().getMapId() + " - " + c.getPlayer().getMap().getMapName());
/*    */       }
/* 65 */       log.error("执行地图脚本过程中发生错误.未找到Portal为:(" + portal.getScriptName() + ".js)的文件 在地图 " + c.getPlayer().getMapId() + " - " + c.getPlayer().getMap().getMapName());
/*    */     } 
/*    */   }
/*    */   
/*    */   public void clearScripts() {
/* 70 */     this.scripts.clear();
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\scripting\portal\PortalScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */